//获取学生数据
var getStuData = function(){

	var storage = window.localStorage;
	var stu = JSON.parse(storage.getItem("stu"));
	return stu;
	
}

//展示数据
var displayData = function(){
	var stu = getStuData();

	var tbody = document.getElementById("index-table");
	var rows = tbody.rows.length-1;

	for(var i=rows;i<stu.length;i++){
		var tr = document.createElement("tr");

		var operate = document.createElement("td");
		operate.innerHTML = 
		"<td><input type='button' name='delBtn' value='删除' onclick='delData(this)'><input type='button' name='editBtn' value='编辑' onclick='editData(this)'></td>";
		var order = document.createElement("td");
		order.innerHTML = i+1;
		order.className="order";
		var stuName = document.createElement("td");
		stuName.innerHTML = stu[i].stuName;
		stuName.className = "stuName";
		var stuNumber = document.createElement("td");
		stuNumber.innerHTML = stu[i].stuNumber;
		stuNumber.className = "stuNumber";
		var stuAge = document.createElement("td");
		stuAge.innerHTML = stu[i].stuAge;
		stuAge.className = "stuAge";
		// console.log(order.nodeType());
		tr.appendChild(operate);
		tr.appendChild(order);
		tr.appendChild(stuName);
		tr.appendChild(stuNumber);
		tr.appendChild(stuAge);

		tbody.appendChild(tr);
	}
}

//保存数据
var saveData = function(stu){
	var storage = window.localStorage;
	storage.setItem("stu",JSON.stringify(stu));

	displayData();
}
var stu=[
		{stuName:"张三",stuNumber:"2017401152",stuAge:20},
		{stuName:"李四",stuNumber:"2017401152",stuAge:21},
	]

	var storage = window.localStorage;
	storage.setItem("stu",JSON.stringify(stu));


//页面初始化
window.onload = function(){
	displayData();
	document.getElementById("stuName").focus();//获取焦点
}

//增加学生信息
var addData = function(){

	var data = getStuData();
	
	var stuName = document.getElementById("stuName").value;
	var stuNumber = document.getElementById("stuNumber").value;
	var stuAge = document.getElementById("stuAge").value;

	var t = check(stuName,stuNumber,stuAge);
		
	if(t == 0){
		return ;
	}

	var stu = {stuName:stuName,stuNumber:stuNumber,stuAge:stuAge};
	
	data.push(stu);
	saveData(data);
	clearInput();

}

//清空输入框
var clearInput = function(){
	document.getElementById("stuName").value = "";
	document.getElementById("stuNumber").value = "";
	document.getElementById("stuAge").value = "";
}

//删除学生数据
var delData = function(e){
	if(confirm("确认删除改行学生信息吗？")){
		var tr = e.closest("tr")
		tr.remove();
		
		var stu = getStuData();
		stu.splice(tr.rowIndex-1,1);
		saveData(stu);
		location.reload('index.heml');//刷新页面
	}
}

//编辑数据
var editData = function(e){
	var tr = e.closest("tr");
	document.getElementById("stuName").value = tr.getElementsByClassName("stuName")[0].innerHTML;
	document.getElementById("stuNumber").value = tr.getElementsByClassName("stuNumber")[0].innerHTML;
	document.getElementById("stuAge").value = tr.getElementsByClassName("stuAge")[0].innerHTML;

	document.getElementById("saveBtn").removeAttribute("disabled");
	document.getElementById("saveBtn").name = tr.rowIndex;
	
}

//保存编辑的数据
var saveEdit = function(e){

	if(confirm("确认保存改信息？")){

		var stuName = document.getElementById("stuName").value;
		var stuNumber = document.getElementById("stuNumber").value;
		var stuAge = document.getElementById("stuAge").value;

		var t = check(stuName,stuNumber,stuAge);

		if(t == 0){
			return ;
		}

		var row = e.getAttribute("name");
		
		var stu = getStuData();

		stu[row-1].stuName = stuName;
		stu[row-1].stuNumber = stuNumber;
		stu[row-1].stuAge = stuAge;

		document.getElementsByClassName("stuName")[row].innerHTML = stuName;
		document.getElementsByClassName("stuNumber")[row].innerHTML = stuNumber;
		document.getElementsByClassName("stuAge")[row].innerHTML = stuAge;
		saveData(stu);
		clearInput();
		document.getElementById("saveBtn").setAttribute("disabled","disabled");
	}

}

var check = function(stuName,stuNumber,stuAge){
	var msg;
	var code;
	if(stuName.length == 0){
		msg = "姓名不能为空!";
		code = 1;
	}else if(stuNumber.length == 0){
		msg = "学号不能为空!";
		code = 2;
	}else if(stuAge.length == 0){
		msg = "年龄不能为空!";
		code = 3;
	}else{
		code = 0;//成功;
		msg = "";
	}

	if(code != 0){
		if(code == 1){
			document.getElementById("msg1").innerHTML = msg;
			document.getElementById("stuName").focus();//获取焦点

		}else if(code == 2){
			document.getElementById("msg2").innerHTML = msg;
			document.getElementById("stuNumber").focus();//获取焦点
		}else{
			document.getElementById("msg3").innerHTML = msg;
			document.getElementById("stuAge").focus();//获取焦点
		}

		return 0;
	}else{

		document.getElementById("msg1").innerHTML = msg;
		document.getElementById("msg2").innerHTML = msg;
		document.getElementById("msg3").innerHTML = msg;

		return 1;
	}
}